﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Inovout.Web.Http
{
    public class MultipartRelatedMediaTypeFormatter : MediaTypeFormatter
    {
        public MultipartRelatedMediaTypeFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/related"));

        }

        public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var multipartRelatedStreamProvider = await content.ReadAsMultipartAsync(new MultipartRelatedStreamProvider());
            return Activator.CreateInstance(type);
            object result = null;

            foreach (var partContent in multipartRelatedStreamProvider.Contents)
            {
                result = await partContent.ReadAsAsync(type);
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }

        private static async Task<object> ReadAsAsync(HttpContent content, Stream stream, Type type, IEnumerable<MediaTypeFormatter> formatters, IFormatterLogger formatterLogger)
        {
            MediaTypeFormatter formatter = null;
            // Default to "application/octet-stream" if there is no content-type in accordance with section 7.2.1 of the HTTP spec
            MediaTypeHeaderValue mediaType = content.Headers.ContentType ?? new MediaTypeHeaderValue("application/octet-stream");

            formatter = new MediaTypeFormatterCollection(formatters).FindReader(type, mediaType);

            if (formatter == null)
            {
                return null;
            }

            return await formatter.ReadFromStreamAsync(type, stream, content, formatterLogger);
        }


        public override bool CanReadType(Type type)
        {
            return true;
        }
        public override bool CanWriteType(Type type)
        {
            return false;
        }
        public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, System.Net.Http.Headers.MediaTypeHeaderValue mediaType)
        {
            return base.GetPerRequestFormatterInstance(type, request, mediaType);
        }
        public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
        {
            return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
        }
        public override void SetDefaultContentHeaders(Type type, System.Net.Http.Headers.HttpContentHeaders headers, System.Net.Http.Headers.MediaTypeHeaderValue mediaType)
        {
            base.SetDefaultContentHeaders(type, headers, mediaType);
        }
    }
}
